package com.kkb.springcloudbill.frontend.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.kkb.billmngfeignclient.clients.BillmngFeign;
import com.kkb.springcloudbill.entity.Bill;
import com.kkb.springcloudbill.entity.BillType;
import com.kkb.springcloudbill.entity.ResultVo;
import com.kkb.springcloudbill.utils.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class InitController {

    @Autowired
    private BillmngFeign billmngFeign;

    @Autowired
    private DateConverter dateConverter;

    @RequestMapping("/")
    public String init(
            @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            Model model) {

        ResponseEntity<ResultVo> entity = billmngFeign.billsNonBill(pageNum, pageSize);
        ResultVo resultVo = entity.getBody();
        HashMap<String, Object> map = (HashMap<String, Object>) resultVo.getData();

        String infoString = JSON.toJSONString(map.get("pageInfo"));
        JSONObject jsonObjectInfo = JSON.parseObject(infoString);
        List list = (List) jsonObjectInfo.get("list");
        List<Bill> bills = JSON.parseArray(JSON.toJSONString(list), Bill.class);
        PageInfo<Bill> pageInfo = JSON.parseObject(String.valueOf(jsonObjectInfo), PageInfo.class);
        pageInfo.setList(bills);
        System.out.println(pageInfo);

        String billTypesString = JSON.toJSONString(map.get("billTypes"));
        List<BillType> billTypes = JSON.parseArray(billTypesString, BillType.class);
        System.out.println(billTypes);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("bill", new Bill());
        model.addAttribute("billTypes", billTypes);
        return "bill/list";
    }

    @RequestMapping(value = "/query")
    public String billtest(@RequestParam(required = false) Bill bill,
                           @RequestParam(name = "pageNum",defaultValue = "1") Integer pageNum,
                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                           Model model) {
        ResponseEntity<ResultVo> entity = null;
        System.out.println(bill);
        if (bill == null) {
            entity = billmngFeign.billsNonBill(pageNum,pageSize);
            bill = new Bill();
        }
        else{
            entity = billmngFeign.billtest(bill, pageNum, pageSize);
        }
        ResultVo resultVo = entity.getBody();
        HashMap<String, Object> map  = (HashMap<String, Object>) resultVo.getData();

        String infoString = JSON.toJSONString(map.get("pageInfo"));
        JSONObject jsonObjectInfo = JSON.parseObject(infoString);
        List list = (List) jsonObjectInfo.get("list");
        List<Bill> bills = JSON.parseArray(JSON.toJSONString(list), Bill.class);
        PageInfo<Bill> pageInfo = JSON.parseObject(String.valueOf(jsonObjectInfo), PageInfo.class);
        pageInfo.setList(bills);
        System.out.println(pageInfo);

        String billTypesString = JSON.toJSONString(map.get("billTypes"));
        List<BillType> billTypes = JSON.parseArray(billTypesString, BillType.class);
        System.out.println(billTypes);

        model.addAttribute("pageInfo", pageInfo);
        System.out.println(bill);
        model.addAttribute("bill", bill);
        model.addAttribute("billTypes", billTypes);
        return "bill/list";
    }

    @RequestMapping(value = "/queryByVo")
    public String queryByVo(
            @RequestParam(required = false) Integer typeId,
            @RequestParam(required = false)  Date start,
            @RequestParam(required = false)  Date end,
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            Model model){
        Bill bill = new Bill();
        if (typeId != null && !"".equals(typeId.toString())) bill.setTypeId(typeId);
        if (start != null && !"".equals(start.toString())) bill.setStart(start);
        if (end != null && !"".equals(end.toString())) bill.setEnd(end);

        System.out.println(bill);
        ResponseEntity<ResultVo> entity = billmngFeign.billtest(bill, pageNum, pageSize);
        ResultVo resultVo = entity.getBody();
        HashMap<String, Object> map  = (HashMap<String, Object>) resultVo.getData();

        String infoString = JSON.toJSONString(map.get("pageInfo"));
        JSONObject jsonObjectInfo = JSON.parseObject(infoString);
        List list = (List) jsonObjectInfo.get("list");
        List<Bill> bills = JSON.parseArray(JSON.toJSONString(list), Bill.class);
        PageInfo<Bill> pageInfo = JSON.parseObject(String.valueOf(jsonObjectInfo), PageInfo.class);
        pageInfo.setList(bills);
        System.out.println(pageInfo);

        String billTypesString = JSON.toJSONString(map.get("billTypes"));
        List<BillType> billTypes = JSON.parseArray(billTypesString, BillType.class);
        System.out.println(billTypes);

        model.addAttribute("pageInfo", pageInfo);
        System.out.println(bill);
        model.addAttribute("bill", bill);
        model.addAttribute("billTypes", billTypes);
        return "bill/list";
    }

    //根据billid获取bill信息
    @RequestMapping(value = "/bill/update/{billId}")
    public String getBillById(
            @PathVariable("billId") Integer billId,
            Model model){
        ResponseEntity<Bill> entity = billmngFeign.getBillById(billId);
        Bill bill = entity.getBody();
        model.addAttribute("bill", bill);
        List<BillType> billTypes = billmngFeign.getAllBillType();
        model.addAttribute("billTypes", billTypes);
        return "/bill/update";
    }

    @RequestMapping(value = "/bill/update")
    public String updateBillById(Bill bill, Model model){
        System.out.println(bill);
        ResponseEntity<Integer> entity = billmngFeign.updateBillById(bill);
        Integer i = entity.getBody();
        return billtest(bill, 1, 10, model);
    }

    //删除账单
    @RequestMapping(value = "/bill/delete/{billId}")
    public String deleteBill(
            @PathVariable("billId") Integer billId,
            Model model){
        billmngFeign.deleteBill(billId);
        return init(1,10,model);
    }

    //加载add.html
    @RequestMapping(value = "/bill/toAdd")
    public String getReadyToAdd(Model model){
        List<BillType> billTypes = billmngFeign.getAllBillType();
        model.addAttribute("billTypes", billTypes);
        return "/bill/add";
    }

    //执行insert新的bill信息
    @RequestMapping(value = "/bill/add")
    public String addBill(Bill bill, Model model){
        System.out.println(bill);
        billmngFeign.addNewBill(bill);
        return init(1, 10, model);
    }
}
